package com.messi.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.messi.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//绑定配置项
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117656457";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC1xv8TfxLHjnpjmYxsbZ0rBK8Af2yYr0Q8aYmlr4fcCzzjdG99znPqCS1VOdcLS8EvSGEdyGH4ulC+idx/FjcmmknjUojGIqYWUq45zUq3anO+YYT5Yfq6PTi2TnDdKzULwLkelh4yTttSQAmRwqjTCvgKYIHdFpxbM0PUBInOYDfZnk57lDTs8dU+c14c2w5AX/Vb3lnwdvmtuWz68Y/hhZMvklFSp6PjOYMvJss31/OU+GpuZxsAfxvpFtZRLFyZ9tZRCgFwYD3aywT1HzvCGX2uglMPhqpdlYgKhwZF1/DSvNA0VASn+rfRHBUGTKs/khs34Rh2ZQ/KJYTVXkBHAgMBAAECggEBAKa197Agxc9hG1wHMeMrcI5IgFtbgfRSIamqPQRIhLhJUjFk0u+1jVdhzUKxhkik1GsR3YecBEZCUdU9yIyBilkL/aHPSwM0OdCVSZZXciPfLIG5HGQ++N0fUsjYDei5PLAns1+a29zVt42qj1wwA/dxwuR5eACVQRbfYGCtPNmYkjbout/RtqCNRCM2fvxikf7ttM+qq903ajd2ktPWRaPCpozUbXp21RUNeos2iOHGGk7KL08moBvnJWrcvk8pC4mGKGiMr66mFqjOJVUW2FwuF/Yq+SVmHT07v9xWqLyTgN4rAdeMPWlru7ln/vMk6ZQWu6BcP9OjhhY6F+wKNBkCgYEA/T+OAytSijE04O+KURB/0Otz28liBKM7c9dNksgJj0GOqXbnXkMSxUIwcpo3F63JWucW9wOYKFW1V/omorc+qk2ELWgJ2xNkWz5JTLhJsIfctJr3rK8hi2jQg/O4uNcXSpox4ORF9pfiv/vn8LqSlb2SsqImQEd1EpsaYhfHiS0CgYEAt8CipETi2/S4+kCHT0im1sfQ2KbVikhTICHh9p1UrsiwmtWdDOKLFGOqkTyke7Q15Dz1VAM6lwPghEeFTZh92/5v/D0YFezszVDUcQf2us+yD/G7dHPllr65eAZE9r1Wl88gsn0hOuaq+5SRKm2wwNWrKZp/F1blWcRwWvLnr8MCgYEAvZXklI7MHdQ0h+gLniBB7380nuclz977VP9rfWTvcyVazZbMIo8H8FBawJCeicV6Jek2EfnI4Pk45wHIBqwz535cYzmtlynvSTdZ3GtiDxvGqBYTMF1zOVBgOUgtk4LCVwEUMg3yKmcOQzcsLdUeg6cFuWEWTayxAhrHJ4QxxokCgYBc+VNR2qkOshyGQvAz5YbFVgUOGhwiA8ZLk9lJdwHKDjGNplf4lQfaITPEmPKAxQYfndhj0d5rWsLXK3j/chD8pxjyf9cYAPEwUZ2sM2Nb0os+FgnLOnkIdShS2pk6X0LZPSxGI5oUu3H4KYvp8+ZjK7mXK1zy6jzR1tsiqZUhEQKBgHM6q/EPEisThJS/IATxIalYQh8KWAqohVNyjN2q6dk5z1sahR5t5kpg+r1RGpKXlT9gxPyekHkWOcydjriS+P+/uHGr5gYyWK30HlVxljf0rhhPVI+wsFbQ1ykjYoVkGSaolGKtN8MFwLUS3e8PxtO9BD/6fupdL9knIsun8TPk";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。

    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzFz/h3QjWrq8De60UiQPmXcpgS6D16C/wI67HAiCcGKDWETPUTlbYZmwmeKHAjAfnP7jLdYUFxQxUM1OLPXcrMZi72SAGST89QrtneD1LWYEmVcxR8pQLfXb/tC9/+x9DMV4P6k/UYqyPA6MV910Z4isnOt9cV/GJbZzrrMGXxQTbUiexC+oe73saF1r1mK6tcDxVBefJ5cmg4irf0AooeYoyKY22dI8u8HJMEeQWbKgr1b+Y8Ac47DDpYoAuciJN/Stwmtdxpt/H/jO4T21ulCiOEfXYV7qnLcSLYhrYV31MaJnlQYT1OnB4bwOLHVfDkQV/21tYfMHrySOCsK7gwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://messiphc.cn.utools.club/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.messimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
